package com.pagepub.expo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import com.pagepub.expo.entity.CorpTemplate;
import com.pagepub.expo.entity.IdName;

@Repository
public class CorpTemplateDao {

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate template;
	
	
	@Autowired
	@Qualifier("appSeqDao")
	private AppSeqDao appSeqDao;
	
	private static String tableName="Corp_Template";
	
	private static String ID_NAME="SELECT id, templateName name FROM "+tableName;
	
	private static String QUERY_ALL = "SELECT id,templateName,realPath,saveDirectory,filePostfix," +
			"isvalId,createDate, modDate,xmlConfig,xmlHead, xmlFoot FROM "+tableName;

	private static String QUERY_ALL_COUNT = "SELECT COUNT(*) FROM "+  tableName;
	
	private static String INSERT_SQL = "INSERT  INTO "+tableName+" (" +
			"id,templateName,realPath,saveDirectory,filePostfix," +
			"isvalId,createDate, modDate,xmlConfig,xmlHead, xmlFoot)VALUES(" +
			":id, :templateName,:realPath,:saveDirectory,:filePostfix," +
			":isvalId,:createDate,:modDate,:xmlConfig,:xmlHead,:xmlFoot)";

	private static String DELETE_SQL = "DELETE FROM "+ tableName +" WHERE id=?";

	private static String UPDATE_SQL = "UPDATE "+ tableName + " SET " +
			"templateName=:templateName,realPath=:realPath,saveDirectory=:saveDirectory,filePostfix=:filePostfix," +
			"isvalId=:isvalId,modDate=:modDate,xmlConfig=:xmlConfig,xmlHead=:xmlHead,xmlFoot=:xmlFoot"+
			" WHERE id=:id";

	private final ParameterizedRowMapper<CorpTemplate> mapper = new ParameterizedRowMapper<CorpTemplate>() {
		public CorpTemplate mapRow(ResultSet rs, int rowNum) throws SQLException {
			CorpTemplate obj = new CorpTemplate();
			obj.setId(rs.getLong("id"));
			obj.setTemplateName(rs.getString("templateName"));
			obj.setRealPath(rs.getString("realPath"));
			obj.setSaveDirectory(rs.getString("saveDirectory"));
			obj.setFilePostfix(rs.getString("filePostfix"));
			obj.setIsvalId(rs.getByte("isvalId"));
			obj.setCreateDate(rs.getTimestamp("createDate"));
			obj.setModDate(rs.getTimestamp("modDate"));
			obj.setXmlConfig(rs.getString("xmlConfig"));
			obj.setXmlFoot(rs.getString("xmlFoot"));
			obj.setXmlHead(rs.getString("xmlHead"));
			return obj;
		}
	};

	public List<CorpTemplate> findAll() {
		return template.query(QUERY_ALL, mapper);
	}

	public CorpTemplate findById(Long id) {
		List<CorpTemplate> list = template.query(QUERY_ALL + " WHERE id=?",
				new Object[] { id }, mapper);
		if (list.size() == 0) {
			return null;
		} else
			return list.get(0);
	}
	
	

	
	public void insert(CorpTemplate bean) {
		if(bean.getId()==null){
			bean.setId(new Long(appSeqDao.getSequence("corp_template_seq")));
		}		
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(INSERT_SQL, ps);

	}

	public void delete(Long id) {
		template.update(DELETE_SQL, id);
	}

	public void update(CorpTemplate bean) {
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(UPDATE_SQL, ps);
	}
	
	public int getCount(Map<String, String> param){
		int count = template.queryForInt(QUERY_ALL_COUNT);
		return count;
	}
	
	public List findByPage(int currentPage,int pageSize,Map<String, String> param){
		List lists = template.query(QUERY_ALL + " limit ?,?",
				new Object[] { currentPage, pageSize}, mapper);
		return lists;
		
	}
	
	public List<IdName> findIdNames(){
		List lists = template.query(ID_NAME, IdName.idNameMapper);
		return lists;
	}
}
